package mil.af.amc.adpetracker.dao.hibernate;

import mil.af.amc.adpetracker.dao.RankDao;
import mil.af.amc.adpetracker.model.Rank;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Hibernate Rank DAO implementation
 *
 * All methods that can change a value are overridden to throw a UOE, since Rank should be a read-only object.
 *
 * @author TSgt Jason Ferguson
 */
@Service("rankDao")
public class HibernateRankDao extends HibernateGenericDao<Rank> implements RankDao {

    public HibernateRankDao() {
        super(Rank.class);
    }

    @Override
    public void delete(Rank persistentObject) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void saveOrUpdate(Rank object) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void save(Rank object) {
        throw new UnsupportedOperationException();
    }

    @Override
    public Integer add(Rank object) {
        throw new UnsupportedOperationException();
    }

    @Override
    public Rank merge(Rank object) {
        throw new UnsupportedOperationException();
    }

    @Override
    public List<Rank> getByQbe(Rank object) {
        throw new UnsupportedOperationException();
    }

    @SuppressWarnings({"unchecked"})
    @Override
    public List<Rank> getAll() {

        DetachedCriteria crit = DetachedCriteria.forClass(Rank.class);

        // Define Restrictions
        // Branch of Service restrictions
        Criterion afEnlistedCrit = Restrictions.and(Restrictions.ge("id", 211), Restrictions.le("id", 220));
        Criterion afCivilianCrit = Restrictions.eq("id", 701);

        crit.add(Restrictions.or(afEnlistedCrit, afCivilianCrit));

        crit.addOrder(Order.asc("sortOrder"));

        return (List<Rank>) getHibernateTemplate().findByCriteria(crit);
    }
}
